<?php
header('content-type:text/html;charset=utf-8');
if (!empty($_POST)) {
    define('IN_TG', 'dologin');
    require ('includes/common.inc.php');
    include ROOT_PATH.'includes/login.func.php';
    session_start();
    _check_code($_POST['code'],$_SESSION['code']);
    $username = sha1(_check_username($_POST['username'],2,20));
    $password = _check_password($_POST['password'],6);
    $time = _check_time($_POST['time']);
    switch ($time) {
        case 0 :
            $lifeTime = 0;
            break;
        case 1 :
            $lifeTime = 24 * 3600;
            break;
        case 2 :
            $lifeTime = 7 * 24 * 3600;
            break;
        case 3 :
            $lifeTime = 30 * 24 * 3600;
            break;
        default :
            break;
    }
    session_set_cookie_params($lifeTime);
    $sql = "select tg_username,tg_skin from tg_user where tg_uniqid = '$username' and tg_password = '$password'";
    $res = _query($sql);
    $row = _fetch_array_list($res);
    setcookie('skin',$row['tg_skin'],time()+1000);
    $num = _num_rows($res);
    if ($num == 1) {
        $sql = "update tg_user set tg_login_count=tg_login_count+1,tg_last_ip='"._get_ip()."',tg_last_time=now() where tg_uniqid = '{$username}'";
        _query($sql);
        $_COOKIE['uniqid'] = $username;
        $_SESSION['username'] = $row['tg_username'];
        _setcookies($_SESSION['username'], $username,$time);
        header('Location:'.$_POST['back_url']);
    } else {
        _alert_back("用户名密码错误");
    }
}
?>